package cc.ufinity.networkdiagnose.widget;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import cc.ufinity.networkdiagnose.adapter.CityiListAdapter;
import cc.ufinity.networkdiagnose.manager.City;
import cc.ufinity.networkdiagnose.view.R;

public class OtherNet extends GeneralTestWidget {

	private Activity ctx;
	private EditText mCity, county, hot;
	private Button save, cancle;
	
	private List<City> citys;
	
	public OtherNet(Activity context) { 
		super(context);
		inflate(context, R.layout.other_net, this);
		this.ctx = context;
		save = (Button)findViewById(R.id.other_net_save);
		cancle = (Button)findViewById(R.id.other_net_cancle);
		mCity = (EditText)findViewById(R.id.other_net_city);
		county = (EditText)findViewById(R.id.other_net_county);
		hot = (EditText)findViewById(R.id.other_net_hot);
		
		save.setOnClickListener(listener);
		cancle.setOnClickListener(listener);
		mCity.setOnClickListener(listener);
		
		init();
	}
	
	OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(v == save) {
				if(mCity.getText().toString().equals("")) {
					Toast.makeText(ctx, "请选择城市", Toast.LENGTH_SHORT).show();
				} else if(county.getText().toString().trim().equals("")) {
					Toast.makeText(ctx, "请输入县市", Toast.LENGTH_SHORT).show();
				} else if(hot.getText().toString().trim().equals("")) {
					Toast.makeText(ctx, "请输入热点", Toast.LENGTH_SHORT).show();
				} else {
					/*TestWidget tw = new TestWidget(ctx);
					ctx.setContentView(tw);
					
					System.out.println("热点信息：" + mCity.getText() + county.getText() + hot.getText());*/
				}
			} else if(v == cancle) {
				MoveOrOther mo = new MoveOrOther(ctx);
				ctx.setContentView(mo);
			} else if(v == mCity) {
				showHotListDialog(citys);
				
				System.out.println("请选择城市名称=========");
			}
		}
	};
	
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if(msg.arg1 == 1) {
				City city = (City)msg.obj;
				mCity.setText(city.getName());
			}
		}
		
	};
	/**
     * 显示热点名称列表
     */
	public void showHotListDialog(final List<City> list) {
    	LayoutInflater factory = LayoutInflater.from(ctx);
		final View dialogView = factory.inflate(R.layout.show_city_list_dialog, null);
		final ListView listView = (ListView) dialogView.findViewById(R.id.hot_list);
//		final List<HotPoint> list = new ArrayList<HotPoint>();
		final CityiListAdapter hotListAdapter = new CityiListAdapter(ctx, list);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				
				if(list.get(arg2).isSelected()) {
					for(City city:list) {
						city.setSelected(false);
					}
				} else {
					for(City city:list) {
						city.setSelected(false);
					}
					list.get(arg2).setSelected(true);
				}
				hotListAdapter.notifyDataSetChanged();
			}
			
		});
		
		listView.setAdapter(hotListAdapter);
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx); 
        builder.setTitle("城市列表");  
//        builder.setIcon(R.drawable.what);
        builder.setView(dialogView); 
        builder.setCancelable(false); 
        builder.setPositiveButton("确  认", new DialogInterface.OnClickListener() {
        	public void onClick(DialogInterface dialog, int id) {
        		for(City city:list) {
        			if(city.isSelected()) {
        				System.out.println("你选择了：" + city.getName());
        				Message msg = handler.obtainMessage();
        				msg.arg1 = 1;
        				msg.obj = city;
        				handler.sendMessage(msg);
        				city.setSelected(false);
    					break;
        			}
				}
        	}
        });

	 	builder.create().show();
    }
	
	private void init() {
		citys = new ArrayList<City>();
		
		City city;
		city = new City("合肥", false);
		citys.add(city);
		city = new City("蚌埠", false);
		citys.add(city);
		city = new City("芜湖", false);
		citys.add(city);
		city = new City("安庆", false);
		citys.add(city);
		city = new City("阜阳", false);
		citys.add(city);
		city = new City("亳州", false);
		citys.add(city);
		city = new City("淮北", false);
		citys.add(city);
		city = new City("淮南", false);
		citys.add(city);
		city = new City("宿州", false);
		citys.add(city);
		city = new City("滁州", false);
		citys.add(city);
		city = new City("宣城", false);
		citys.add(city);
		city = new City("黄山", false);
		citys.add(city);
		city = new City("马鞍山", false);
		citys.add(city);
		city = new City("六安", false);
		citys.add(city);
		city = new City("铜陵", false);
		citys.add(city);
		city = new City("池州", false);
		citys.add(city);
		
		city = null;
	}
	
}
